package com.attms.service.impl;

import com.attms.dao.AbsentDao;
import com.attms.dao.EmployeeDao;
import com.attms.domain.Absent;
import com.attms.domain.Employee;
import com.attms.service.IDManagerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.util.Date;
import java.util.List;

import static java.time.temporal.ChronoUnit.DAYS;
import static org.apache.commons.lang3.compare.ComparableUtils.ge;

@Service
public class DManagerServiceImpl extends ServiceImpl<AbsentDao, Absent> implements IDManagerService {
    @Autowired
    private AbsentDao absentDao;
    @Autowired
    private EmployeeDao employeeDao;
    @Override
    /**
     * 处理请假，同意与否，假条id，回复信息
     */
    public boolean solveLeave(Integer isagree,Absent absenter){
        System.out.println(isagree + " " + absenter.getA_ID() + " " + absenter.getA_RESPON());
        LambdaQueryWrapper<Absent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Absent::getA_ID, absenter.getA_ID());
        Absent absence = absentDao.selectOne(wrapper);
        System.out.println(absence);
        //如果部门经理同意，则依据类型和状态选择转1总经理审批 2人事审核  3申请通过4申请失败
        if(isagree == 0){
            absence.setA_RESPON(absenter.getA_REASON());
            absence.setA_STATE(4);
        }
//        else if(absence.getA_TYPE().equals(4)){//病假。则先交人事
//            absence.setA_RESPON("病假，材料交由人事部审核");
//            absence.setA_STATE(2);
//        }
        else if(absence.getA_ENDTIME().until(absence.getA_STTIME()).getDays()>3){
            absence.setA_RESPON("时长超过三天，部门经理已经同意，交由总经理处理");
            absence.setA_STATE(1);
        }else{
            absence.setA_RESPON("部门经理已经同意审核通过");
            absence .setA_STATE(2);
//            int days = absence.getA_ENDTIME().until(absence.getA_STTIME()).getDays();
//            //如果是因公请假，则不扣薪，只记录
//            Employee nowperson = employeeDao.selectById(absence.getE_ID());
//            if(absence.getA_TYPE()==8){//如果事假，则记录扣薪时长
//                int beforeAbs = nowperson.getE_ABSHOUR();
//                nowperson.setE_ABSHOUR(beforeAbs + days * 8);
//            }else if(absence.getA_TYPE() > 0){//如果是带薪假期，则只修改请的假的剩余天数
//                int vocationType = absence.getA_TYPE();
//                if(vocationType == 1)nowperson.setE_RYEAR(nowperson.getE_RYEAR() - days);
//                else if(vocationType == 2)nowperson.setE_RILL(nowperson.getE_RILL() - days);
//                else if(vocationType == 3)nowperson.setE_RWEDDING(nowperson.getE_RWEDDING() - days);
//                else if(vocationType == 5)nowperson.setE_RMATERNITY(nowperson.getE_RMATERNITY() - days);
//                else if(vocationType == 6)nowperson.setE_RLACTATION(nowperson.getE_RLACTATION() - (int)Duration.between(absence.getA_STTIME(), absence.getA_ENDTIME()).toHours());
//                else if(vocationType == 7)nowperson.setE_RPATERNITY(nowperson.getE_RPATERNITY() - days);
//                employeeDao.updateById(nowperson);
//
//            }
        }
        absentDao.updateById(absence);
        return true;
    }
    //部门经理生成个人请假记录(按照时间，默认为全时间段)
    public List<Absent> getPersonalAbsentList(Absent absent){
        LambdaQueryWrapper<Absent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Absent::getE_ID, absent.getE_ID())
                .ge(Absent::getA_STTIME, absent.getA_STTIME())
                .le(Absent::getA_ENDTIME, absent.getA_ENDTIME());
        return absentDao.selectList(wrapper);
    }
    //部门经理生成本部分请假记录(按照时间，默认为全时间段)
    public List<Absent> getAllAbsentList(Integer department, Absent absent) {
        LambdaQueryWrapper<Absent> wrapper = new LambdaQueryWrapper<>();
        wrapper.ge(Absent::getA_STTIME, absent.getA_STTIME())
                .le(Absent::getA_ENDTIME, absent.getA_ENDTIME())
                .eq(Absent::getA_DEPARTMENT, department);
        return absentDao.selectList(wrapper);
    }


}
